<template>
	<view class="page">
		<image src="https://www.retoc.xyz/icon/leave.png" mode="widthFix" class="logo"></image>
		<view class="add" v-if="checkPermission(['ROOT','MEETING:INSERT'])" @tap="toLeavePage(null,'insert')">
			<image src="https://www.retoc.xyz/icon/icon-17.png" mode="widthFix" class="icon"></image>
			<text>创建请假单</text>
		</view>
		<view class="switch">
			<view class="s1">{{old?'新':'旧'}}</view>
			<switch color="#ffaaff" :checked="old" @change="switchOldData"></switch>
		</view>
		
		<view v-for="one in list" :key="one.id">
			<view class="list-title" >
				{{one.start}}
			</view>
			<view class="item" @longpress="deleteLeaveById(one.id,one.date,one.start)">
				<view class="header">
					<view class="left">
						<image src="https://www.retoc.xyz/icon/icon-11.png" mode="widthFix" class="icon" v-if="one.type=='线上会议'"></image>
						<image src="https://www.retoc.xyz/icon/icon-12.png" mode="widthFix" class="icon" v-if="one.type=='线下会议'"></image>
						<text>{{one.type}}</text>
						<text :class="one.status=='已审批'?'blue':'red'">--- {{one.status}} </text>
					</view>
					<view class="right" @tap="toLeavePage(one.id,'edit')" 
					v-if="checkPermission(['ROOT','MEETING:UPDATE']) && one.status=='未审批'">
						<text>编辑</text>
					</view>
				</view>
				<view class="content">
					<view class="t">
						<view class="title">
							{{one.title}}
						</view>
						<view class="rtitle">
							{{one.hour}}
						</view>
					</view>
				
					<view class="attr">
						<view class="timer">
							<image src="https://www.retoc.xyz/icon/icon-14.png" mode="widthFix" class="icon"></image>
							<text>{{one.start}} ~ {{one.end}}</text>
						</view>
					</view>
					<view class="desc">
						{{one.reason}}
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				page: 1,
				length: 20,
				old:true,
				
				list: [
					// {
					// 	date: '2021年02月08日',
					// 	list: [
					// 		{ id: 1, date: '2021/02/08', start: '08:30', end: '10:30', type: '线上会议', name: '张爽',place:"网络会议室", status:"开始",title:'年终大会', desc:"年终总结大会" },
					// 		{ id: 2, date: '2021/02/08', start: '08:30', end: '10:30', type: '线下会议', name: '张爽',place:"网络会议室", status:"未开始",title:'年终大会', desc:"年终总结大会" }
					// 	]
					// },
					// {
					// 	date: '2021年02月09日',
					// 	list: [
					// 		{ id: 3, date: '2021/02/09', start: '08:30', end: '10:30', type: '线上会议', name: '张爽',place:"网络会议室", status:"未开始",title:'年终大会', desc:"年终总结大会" }
					// 	]
					// }
				],
				isLastPage: false
			};
	
		},
		onShow() {
			// uni.setNavigationBarTitle({
			// 	title:"会议列表"
			// })
			let that=this
			that.page=1;
			that.isLastPage=false
			that.list=[]
			that.loadLeaveList(that)
			console.log("新旧",that.old?'旧':'新');
			console.log("old",that.old);
		},
		onReachBottom() {
			let that=this
			if(that.isLastPage){
				return
			}
			that.page=that.page+1
			that.loadLeaveList(that)
		},
		methods: {
			loadLeaveList:function(ref){
				let data={
					page:ref.page,
					length:ref.length,
					old:ref.old,
				}
				ref.ajax(ref.url.searchMyLeaveListByPage,"POST",data,function(resp){
					let result=resp.data.result
					// console.log('result:',result);
					if(result==null||result.length==0){
						ref.isLastPage=true
						ref.page=ref.page-1
						uni.showToast({
							icon:'none',
							title:'没有下一页了'
						})
					}
					else{
						for(let leave of result){
							if(leave.type==1){
								leave.type="事假"
							}else if(leave.type==2){
								leave.type="病假"
							}else if(leave.type==3){
								leave.type="年假"
							}
							
							if(leave.status==0){
								leave.status="未审批"
							}else if(leave.status==1){
								leave.status="已审批"
							}else if(leave.status==2){
								leave.status="已被拒绝"
							}
							
							if(leave.hour<8){
								leave.hour=leave.hour+"小时"
							}else{
								let day=Math.floor(leave.hour/8)
								let h=leave.hour%8
								if(h==0){
									leave.hour=day+"天 "
								}else{
									leave.hour=day+"天 "+h+"小时"
								}
							}
							ref.list.push(leave)
						}
					}
				})
			},
			switchOldData(e){
				let that=this;
				that.page=1;
				that.old=e.detail.value
				that.list=[]
				// console.log(that.old);
				that.loadLeaveList(that)
				
				// console.log("新旧",that.old?'旧':'新');
				// console.log("old",that.old);
			},
			
			toLeavePage(id,opt){
				// console.log('list.id:',id);
				uni.navigateTo({
					url:"../leave/leave?id="+id+"&opt="+opt
				})
			},
			deleteLeaveById(id,date,start){
				let now = new Date();
				let meetingDate = new Date(date + ' ' + start + ':00');
				if (now.getTime() >= meetingDate.getTime() - 20 * 60 * 1000) {
					uni.showToast({
						title: '该会议无法删除',
						icon: 'none'
					});
					return;
				}
				let that = this;
				uni.vibrateShort({});
				uni.showModal({
					title: '提示信息',
					content: '是否删除这个请假？',
					success: function(resp) {
						if (resp.confirm) {
							let data = {
								id: id
							};
							that.ajax(that.url.deleteMeetingById, 'POST', data, function(resp) {
								uni.showToast({
									icon: 'success',
									title: '删除成功',
									complete: function() {
										setTimeout(function() {
											that.page = 1;
											that.isLastPage = false;
											uni.pageScrollTo({
												scrollTop: '0'
											});
											that.list=[]
											that.loadLeaveList(that);
										}, 2000);
									}
								});
							});
						}
					}
				});
			}
		}
	}
</script>

<style lang="less">
	@import url(leave_list.less);
</style>
